package com.czy.owner.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.TextView;

import com.czy.owner.R;
import com.czy.owner.entity.UserLocationModel;
import com.easyrecycleview.adapter.BaseViewHolder;
import com.easyrecycleview.adapter.RecyclerArrayAdapter;


/**
 * Created by wangyang on 2017/5/18 0018.
 */

public class AddressAdapter extends RecyclerArrayAdapter<UserLocationModel.ListBean> {
    private Context context;
    private OnItemSetDefaultListener onItemSetDefaultListener;
    private OnItemEditListener onItemEditListener;
    private OnItemDeleteListener onItemDeleteListener;
    private boolean hasArgument2;

    public AddressAdapter(Context context, boolean hasArgument2) {
        super(context);
        this.context = context;
        this.hasArgument2 = hasArgument2;
    }


    @Override
    public BaseViewHolder OnCreateViewHolder(ViewGroup parent, int viewType) {
        return new AddressViewHolder(parent);
    }

    class AddressViewHolder extends BaseViewHolder<UserLocationModel.ListBean> {
        private TextView name;
        private TextView phone;
        private TextView address;
        private RadioButton rbItemDefault;
        private ImageView ivItemDelete;
        private ImageView ivItemEdit;

        public AddressViewHolder(ViewGroup parent) {
            super(parent, R.layout.item_receiver_address);
            name = $(R.id.tv_user_name);
            phone = $(R.id.tv_user_phone);
            address = $(R.id.tv_detail_address);
            rbItemDefault = $(R.id.rb_item_default);
            ivItemDelete = $(R.id.iv_item_delete);
            ivItemEdit = $(R.id.iv_item_edit);
        }

        @Override
        protected int getDataPosition() {
            return super.getDataPosition();
        }

        @Override
        public void setData(final UserLocationModel.ListBean data) {
            super.setData(data);
            if (hasArgument2) {
                ivItemDelete.setVisibility(View.GONE);
            } else {
                ivItemDelete.setVisibility(View.VISIBLE);
            }
            name.setText(data.getContactUser());
            phone.setText(data.getContact());
            if (TextUtils.isEmpty(data.getDetailLocation())) {
                address.setText("");
                address.setHint("详细地址位未完善");
            } else {
                address.setText(data.getDetailLocation());
            }

            rbItemDefault.setChecked(Boolean.valueOf(data.getIsDefault()));
            rbItemDefault.setTag(data);
            rbItemDefault.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (onItemSetDefaultListener != null && rbItemDefault.isChecked() && data.getIsDefault().equals("false")) {
                        onItemSetDefaultListener.onSet(data, getDataPosition());

                    }
                }
            });

            ivItemEdit.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (onItemEditListener != null) {
                        onItemEditListener.onEdit(data);
                    }
                }
            });

            ivItemDelete.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (onItemDeleteListener != null) {
                        onItemDeleteListener.onDelete(data, getDataPosition());
                    }
                }
            });
        }
    }

    public interface OnItemSetDefaultListener {
        void onSet(UserLocationModel.ListBean data, int position);
    }

    public interface OnItemEditListener {
        void onEdit(UserLocationModel.ListBean data);
    }

    public interface OnItemDeleteListener {
        void onDelete(UserLocationModel.ListBean data, int position);
    }

    public void setOnItemSetDefaultListener(OnItemSetDefaultListener onItemSetDefaultListener) {
        this.onItemSetDefaultListener = onItemSetDefaultListener;
    }

    public void setOnItemEditListener(OnItemEditListener onItemEditListener) {
        this.onItemEditListener = onItemEditListener;
    }

    public void setOnItemDeleteListener(OnItemDeleteListener onItemDeleteListener) {
        this.onItemDeleteListener = onItemDeleteListener;
    }

}
